#include "blur_interface.h"
#include <SDL2/SDL.h>
#include "blur_gpu_gl.h"  // GPU 版本
#include "det_platform.h"

static void apply_row_average_blur(SDL_Surface *src, SDL_Surface *dst) 
{
    int width = src->w;
    int height = src->h;

    Uint32 *src_pixels = (Uint32 *)src->pixels;
    Uint32 *dst_pixels = (Uint32 *)dst->pixels;

    for (int y = 0; y < height; y++) {
        float r_sum = 0, g_sum = 0, b_sum = 0;

        // 计算当前行所有像素的 RGB 平均值
        for (int x = 0; x < width; x++) {
            Uint32 pixel = src_pixels[y * width + x];
            Uint8 r, g, b;
            SDL_GetRGB(pixel, src->format, &r, &g, &b);
            r_sum += r;
            g_sum += g;
            b_sum += b;
        }

        Uint8 avg_r = (Uint8)(r_sum / width);
        Uint8 avg_g = (Uint8)(g_sum / width);
        Uint8 avg_b = (Uint8)(b_sum / width);

        // 将该行所有像素设为平均颜色
        for (int x = 0; x < width; x++) {
            dst_pixels[y * width + x] = SDL_MapRGB(dst->format, avg_r, avg_g, avg_b);
        }
    }
}
void apply_blur(SDL_Surface *src, SDL_Surface *dst) 
{
    // static bool use_gpu = []() {
    //     if (is_rk35xx()) {
    //         printf("Detected RK35xx platform: using GPU blur\n");
    //         return true;
    //     } else if (has_gpu_device()) {
    //         printf("Detected GPU device: using GPU blur\n");
    //         return true;
    //     } else {
    //         printf("No GPU detected: using CPU blur\n");
    //         return false;
    //     }
    // }();

    // if (use_gpu) {
    //     apply_blur_gpu(src, dst);
    // } else {
    //     apply_row_average_blur(src, dst);
    // }

    apply_row_average_blur(src, dst);
}